package eiisan.config.security.util;

import eiisan.dao.result.user.PermissionInfo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 权限信息工具类
 */
public class AuthorityUtil {

	/**
	 * 生成权限信息
	 */
	public static List<GrantedAuthority> make(Collection<PermissionInfo> authorities) {
		return authorities.stream().map(authority -> new SimpleGrantedAuthority(authority.getName())).collect(Collectors
			.toList());
	}
	/**
	 * 生成权限信息
	 */
	public static List<GrantedAuthority> make(String... authorities) {
		return Stream.of(authorities).map(SimpleGrantedAuthority::new).collect(Collectors.toList());
	}

	/**
	 * 获取权限信息
	 */
	public static String[] get(UserDetails user) {
		return user.getAuthorities().stream().map(GrantedAuthority::getAuthority).toArray(String[]::new);
	}

}
